/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
       /* Room outside, theater, pub, lab, office, bigroom, goodroom;
      
        // create the rooms
        outside = new Room("outside the main entrance of the university");
        theater = new Room("in a lecture theater",new Thing("mike",20));
        pub = new Room("in the campus pub",new Thing("gunpowder",-50));
        lab = new Room("in a computing lab");
        office = new Room("in the computing admin office");
        bigroom = new Room("in a big room",new Thing("pill",-100));
        goodroom = new Room("in a good room");
        
        // initialise room exits
        //outside.setExits(null, theater, lab, pub,bigroom,null);
        outside.setExit("east",theater);
        outside.setExit("south",lab);
        outside.setExit("west",pub);
        outside.setExit("up",bigroom);
        //theater.setExits(null, null, null, outside,null,null);
        theater.setExit("west",outside);
        //pub.setExits(null, outside, null, null,null,goodroom);
        pub.setExit("east",outside);
        pub.setExit("down",goodroom);
        //lab.setExits(outside, office, null, null,null,null);
        lab.setExit("north",outside);
        lab.setExit("east",office);        
        //office.setExits(null, null, null, lab,null,null);
        office.setExit("west",lab);
        //goodroom.setExits(null,null,null,null,pub,null);
        goodroom.setExit("up",pub);
        //bigroom.setExits(null,null,null,null,null,outside);
        bigroom.setExit("down",outside);

        currentRoom = outside;  // start game outside
        */
       Room parlour1,pianoroom,room1,toilet1,parlour2,
       sanctum,room2,toilet2,tunnel1,tunnel2,parlour3,pitfall,
       playroom,tunnel3,garden,pitfall2,kitchen,stairs1,stairs2,home;
       // create the rooms
       room1=new Room("在三楼的房间。");
       parlour1=new Room("在三楼的客厅。");
       pianoroom=new Room("在三楼的琴房。", new Thing("pastry",25));
       toilet1=new Room("在三楼的卫生间。", new Thing("water",40));
       stairs1=new Room("在连接三楼和二楼的楼梯上。");
       parlour2=new Room("在二楼的客厅。");
       pitfall=new Room("掉入陷阱！快想办法离开这里！");
       sanctum=new Room("在二楼的书房。", new Thing("pian",-30));
       room2=new Room("在二楼的房间。");
       toilet2=new Room("在二楼的卫生间。", new Thing("pastry",25));
       tunnel1=new Room("进入密道。");
       garden=new Room("在房子外的花园里。");
       pitfall2=new Room("掉入陷阱！快想办法离开这里！");
       tunnel2=new Room("进入密道。");
       parlour3=new Room("在一楼的客厅。", new Thing("pastry",25));
       playroom=new Room("在一楼的游戏室。");
       tunnel3=new Room("进入密道。");
       kitchen=new Room("在一楼厨房。", new Thing("pian",-30));
       stairs2=new Room("在连接二楼和一楼的楼梯上。");
       home=new Room("你已到达鼠洞！");
      // initialise room exits
      room1.setExit("east", parlour1);
      parlour1.setExit("east", toilet1);
      parlour1.setExit("north", pianoroom);
      parlour1.setExit("west", room1);
      parlour1.setExit("south", stairs1);
      toilet1.setExit("west", parlour1);
      pianoroom.setExit("south", parlour1);
      stairs1.setExit("up", parlour1);
      stairs1.setExit("down", parlour2);
      parlour2.setExit("north", stairs1);
      parlour2.setExit("west", room2);
      parlour2.setExit("east", sanctum);
      parlour2.setExit("south", stairs2);
      sanctum.setExit("west", parlour2);
      stairs2.setExit("up", parlour2);
      stairs2.setExit("down", parlour3);
      sanctum.setExit("east", pitfall);
      pitfall.setExit("west", sanctum);
      sanctum.setExit("south",tunnel1);
      tunnel1.setExit("right",sanctum);
      tunnel1.setExit("left",toilet2);
      toilet2.setExit("north",tunnel1);
      room2.setExit("north",tunnel2);
      room2.setExit("east",parlour2);
      tunnel2.setExit("right",garden);
      tunnel2.setExit("left",room2);
      garden.setExit("north",tunnel2);
      garden.setExit("east",parlour3);
      garden.setExit("west",pitfall2);
      pitfall2.setExit("east",garden);
      parlour3.setExit("north",stairs2);
      parlour3.setExit("west",garden);
      parlour3.setExit("east",playroom);
      parlour3.setExit("south",kitchen);
      kitchen.setExit("north",parlour3);
      playroom.setExit("west",parlour3);
      playroom.setExit("south",home);
      kitchen.setExit("west",tunnel3);
      tunnel3.setExit("right",kitchen);
      tunnel3.setExit("left",home);
      home.setExit("east",tunnel3);
      home.setExit("north",playroom);
      currentRoom = room1;// start game room1


} 

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
            finished = gameOver(finished);
        }
        System.out.println("您最终的得分为：" + (player.getScore() + player.getStrength() * 2)  + "！");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到杰克寻家游戏！");
        System.out.println("这是一个考验直觉和运气的游戏。");
        System.out.println("如果需要帮助，请敲击'help' 。");
        System.out.println("...");
        System.out.println("天快亮了，你必须尽快回到鼠洞，否则会被房主发现。");
        System.out.println("途中会消耗体力，你必须寻找食物充饥。");
        System.out.println("注意：房间里有陷阱，它会让你受伤或是失去生命！");
        System.out.println("你现在" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("我不理解你的意思是什么");
            return false;
        }

        Word commandWord = command.getCommandWord();
        /*if (commandWord.equals("help")) {
            printHelp();
        }
        else if (commandWord.equals("go")) {
            goRoom(command);
        }
        else if (commandWord.equals("quit")) {
            wantToQuit = quit(command);
        }
        else if (commandWord.equals("look")) {
            looks();
        }
        else if (commandWord.equals("pick")) {
            picks();
        }
        else if (commandWord.equals("eat")) {
            eats(command);
        }
        else if (commandWord.equals("check")) {
            checks();
        }
        return wantToQuit;
        */
       switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case LOOK:
                lookRoomThings(currentRoom);
                break;
            case CHECK:
                player.checkBag();
                break;
            case EAT:
                eatThing(command);
                break;
            case PICK:
                pickThingUp(currentRoom);
                break;
            case STRENGTH:
                System.out.println("你的当前的生命值为： " + player.getStrength() + "。");
                break;
            }
                return wantToQuit;
    }

    // implementations of user commands:

    
    
    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("时间不多了，你必须要尽快回去。");
        System.out.println("...");
        System.out.println("你可以打算：");
        System.out.println("");
        System.out.println("    " + CommandWords.allCommandWords());
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("你想去哪里？");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goRoom(direction);
        

        if (nextRoom == null) {
            System.out.println("无路可走！");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你现在 " + currentRoom.getDescription());
           if(currentRoom.equalsPitfall()){
                player.loseStrength();
            }
            currentRoom.printExits();
        }
    }
    
    private boolean gameOver(boolean finished) 
    {
        /*if(currentRoom == bedroom && player.getStrength() >= 60 ){
             System.out.println("You have successfully completed the task. You win!");
        }*/
        if(finished){
            System.out.println("感谢你的游玩，再见！");
        }
        if(player.getStrength() <= 0){
            System.out.println("你的生命值耗尽了，你输了！");
            finished = true;
        }
        if(currentRoom.equalsHome()){  
            if(player.getStrength() >= 40) {
                System.out.println("你成功回到了鼠洞，祝贺你游戏通关了!"); 
                finished = true;  
            }
            else {
                System.out.println("虽然你成功回到了鼠洞，但是你的生命值太低，你需要进食。");
            }
            
        }
        return finished;    
    }
    /*private void eats(Command command){
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("Eat what????");
            return;
        }
        String foot = command.getSecondWord();
        t.eat(foot);
    }
    
    private void looks(){
        System.out.println(currentRoom.getThing());
    }
    
    Player t=new Player();
    private void picks(){
        t.pick(currentRoom.thing);
        currentRoom.removeThing();
    }
    
    private void checks(){
        t.checkss();
    }
*/
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("你想要？");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    private void lookRoomThings(Room currentRoom){
        Thing thing = currentRoom.getThing();
        if(thing != null) {
            System.out.print("在当前房间中，你找到了 "+ thing.getName() + " 。");
            System.out.println();  
            System.out.println("你可以把它捡起来。");
        }
        else {
            System.out.println("这个房间什么也找不到。");
        }
    }
    private void pickThingUp(Room currentRoom){ 
        Thing thing = currentRoom.getThing();
        if(thing != null) {            
            player.pick(thing);
            System.out.println("你捡起了： " + thing + " 。");
            System.out.println("现在你可以食用它或者放入包里。");
            currentRoom.removeThing();
        }
        else {
            System.out.println("这里什么都没有。");
        }
    }
    
    private void eatThing(Command command) 
    {
        if(!command.hasSecondWord()) {
            System.out.println("哪一个？");
            return;
        }
        String thingName = command.getSecondWord();
        player.eat(thingName);
    }
}

     